package com.cntcdt.api.db.dao;

import java.util.ArrayList;
import java.util.List;

import com.cntcdt.api.db.dao.base.UpdateGroupDeviceDaoBase;
import com.cntcdt.api.db.model.UpdateGroupDeviceModel;

/**
 * 这是cdt_update_group_device的Dao实现类
 */
public class UpdateGroupDeviceDao extends UpdateGroupDeviceDaoBase {
	/**
	 * UpdateGroupDeviceDao对象
	 */
	private static UpdateGroupDeviceDao instance = new UpdateGroupDeviceDao();

	/**
	 * 默认构造方法
	 */
	private UpdateGroupDeviceDao() {
	}

	/**
	 * 获得对象实例
	 */
	public static UpdateGroupDeviceDao getInstance() {
		return instance;
	}

	public List<UpdateGroupDeviceModel> findByGroupId(int groupId) {
		String sql = "SELECT * FROM " + TABLE + " WHERE groupId = ? and opStatus = 0";
		Object[] paramObjs = { groupId };
		List<UpdateGroupDeviceModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	public UpdateGroupDeviceModel findByGroupIdAndTerminal(int groupId, String terminal) {
		String sql = "SELECT * FROM " + TABLE + " WHERE groupId = ? and terminal = ? and opStatus = 0";
		Object[] paramObjs = { groupId, terminal };
		List<UpdateGroupDeviceModel> modelList = this.queryModelList(sql, paramObjs);
		if (modelList.size() > 0) {
			return modelList.get(0);
		}
		return null;
	}

	/**
	 * 按机器号查询
	 * 
	 * @param terminal
	 * @return
	 */
	public UpdateGroupDeviceModel findByTerminal(String terminal) {
		String sql = "select * from " + TABLE + " where terminal= ? and opStatus = 0";
		Object[] paramObjs = { terminal };
		List<UpdateGroupDeviceModel> modelList = this.queryModelList(sql, paramObjs);
		if (modelList.size() > 0) {
			return modelList.get(0);
		} else {
			return null;
		}
	}

	/**
	 * windows改android38台设备号
	 * 
	 * @return
	 */
	public List<String> windows2android() {
		List<String> terminalList = new ArrayList<String>();
		String sql = "SELECT * FROM " + TABLE + " WHERE groupId = 25 AND opStatus = 0";
		List<UpdateGroupDeviceModel> modelList = this.queryModelList(sql);
		for (UpdateGroupDeviceModel model : modelList) {
			terminalList.add(model.getTerminal());
		}
		return terminalList;
	}

	/**
	 * 是否存在记录
	 * 
	 * @param terminal
	 * @return
	 */
	public boolean isExistRecord(String terminal) {
		String sql = "select * from " + TABLE + " where terminal = ? and groupId = 26 and opStatus = 0";
		Object[] paramObjs = { terminal };
		List<UpdateGroupDeviceModel> modelList = this.queryModelList(sql, paramObjs);
		if (modelList.size() > 0) {
			return true;
		} else {
			return false;
		}
	}
}
